﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace MediaCenterCommander.UserAgent.Util
{
    public static class StartupRegistrationHelper
    {
        private const string AutoStartRegistryKeyName = @"Software\Microsoft\Windows\CurrentVersion\Run";
        private const string ProgramNameValue = "MediaCenterCommander UserAgent";

        public static void RegisterToStartAtLogin()
        {            
            string pathToProgram = typeof(StartupRegistrationHelper).Assembly.Location;
            string startupCommand = "\"" + pathToProgram + "\" /tray";

            using (var key = Registry.CurrentUser.CreateSubKey(AutoStartRegistryKeyName))
            {
                key.SetValue(ProgramNameValue, startupCommand);
            }
        }

        public static void UnregisterToStartAtLogin()
        {
            using (var key = Registry.CurrentUser.CreateSubKey(AutoStartRegistryKeyName))
            {
                key.DeleteValue(ProgramNameValue, false);
            }
        }

        public static bool IsRegisteredToStartAtLogin
        {
            get
            {
                using (var key = Registry.CurrentUser.OpenSubKey(AutoStartRegistryKeyName))
                {
                    if (key != null)
                    {
                        string value = key.GetValue(ProgramNameValue) as string;
                        if (value != null)
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
        }
    }
}
